//
//  SSGoodsListTopButtonView.m
//  textDemo
//
//  Created by Esther on 2017/2/21.
//  Copyright © 2017年 Esther. All rights reserved.
//

#import "SSGoodsListTopButtonView.h"

@interface SSGoodsListTopButtonView()
@property(nonatomic, strong) UIView *backView;
@property(nonatomic, strong) UIButton *hostButton;
@property(nonatomic, strong) UIButton *priceButton;
@property(nonatomic, strong) UIButton *goodButton;
@property(nonatomic, strong) UIButton *newsbutton;
@property(nonatomic, strong) UILabel *lineLabel;

@property(nonatomic, strong) UIButton *oldButton;
@end

@implementation SSGoodsListTopButtonView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.backView];
        [self addSubview:self.hostButton];
        [self addSubview:self.priceButton];
        [self addSubview:self.goodButton];
        [self addSubview:self.newsbutton];
        [self addSubview:self.lineLabel];
        
        #pragma mark - 约束
        [self addLayout];
        
        self.oldButton = self.hostButton;
        
        
    }
    return self;
}


#pragma mark -
#pragma mark - 约束
- (void)addLayout{
    
    __weak typeof (self) weakSelf = self;
    [_backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf);
    }];
    [_hostButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(weakSelf);
        make.width.mas_offset([UIScreen ss_screenWidth] / 4);
        
    }];
    
    [_priceButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(weakSelf);
        make.width.mas_offset([UIScreen ss_screenWidth] / 4);
        make.left.equalTo(weakSelf.hostButton.mas_right);
    }];
    
    [_goodButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(weakSelf);
        make.width.mas_offset([UIScreen ss_screenWidth] / 4);
        make.left.equalTo(weakSelf.priceButton.mas_right);
    }];
    
    [_newsbutton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(weakSelf);
        make.width.mas_offset([UIScreen ss_screenWidth] / 4);
        make.left.equalTo(weakSelf.goodButton.mas_right);
    }];
    
    [_lineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(40, 2));
        make.bottom.equalTo(weakSelf);
        make.centerX.equalTo(self.hostButton.mas_centerX);
        
    }];
    
}


#pragma mark -
#pragma mark - 监听
- (void)fourButtonTouchMetod:(UIButton *)button{
    button.selected = YES;
    self.oldButton.selected = NO;
    self.oldButton = button;
    
   [_lineLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
       make.size.mas_equalTo(CGSizeMake(40, 2));
       make.bottom.equalTo(self);
       make.centerX.equalTo(button.mas_centerX);
       
   }];
    
    [UIView animateWithDuration:0.25 animations:^{
        [self layoutIfNeeded];
        
    }];
}

#pragma mark -
#pragma mark - 懒加载

- (UIView *)backView{
    if (!_backView) {
        _backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen ss_screenWidth], 30)];
        _backView.backgroundColor = [UIColor whiteColor];
        
    }
    return _backView;
}
- (UIButton *)hostButton{
    if (!_hostButton) {
        
        _hostButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_hostButton setTitle:@"热门" forState:(UIControlStateNormal)];
        [_hostButton setTitleColor:[UIColor ss_colorWithRed:162 green:162 blue:162] forState:(UIControlStateNormal)];
        [_hostButton setTitleColor:[UIColor ss_colorWithRed:53 green:161 blue:241] forState:(UIControlStateSelected)];
        [_hostButton addTarget:self action:@selector(fourButtonTouchMetod:) forControlEvents:(UIControlEventTouchUpInside)];
        _hostButton.selected = YES;
    }
    return _hostButton;
}

- (UIButton *)priceButton{
    if (!_priceButton) {
        _priceButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_priceButton setTitle:@"价格" forState:(UIControlStateNormal)];
        [_priceButton setTitleColor:[UIColor ss_colorWithRed:162 green:162 blue:162] forState:(UIControlStateNormal)];
        [_priceButton setTitleColor:[UIColor ss_colorWithRed:53 green:161 blue:241] forState:(UIControlStateSelected)];
        [_priceButton addTarget:self action:@selector(fourButtonTouchMetod:) forControlEvents:(UIControlEventTouchUpInside)];
        _priceButton.selected = NO;
        
    }
    return _priceButton;
}
- (UIButton *)goodButton{
    if (!_goodButton) {
        _goodButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_goodButton setTitle:@"商品" forState:(UIControlStateNormal)];
        [_goodButton setTitleColor:[UIColor ss_colorWithRed:162 green:162 blue:162] forState:(UIControlStateNormal)];
        [_goodButton setTitleColor:[UIColor ss_colorWithRed:53 green:161 blue:241] forState:(UIControlStateSelected)];
        [_goodButton addTarget:self action:@selector(fourButtonTouchMetod:) forControlEvents:(UIControlEventTouchUpInside)];
        _goodButton.selected = NO;
    }
    return _goodButton;
}

- (UIButton *)newsbutton{
    if (!_newsbutton) {
        _newsbutton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_newsbutton setTitle:@"新品" forState:(UIControlStateNormal)];
        [_newsbutton setTitleColor:[UIColor ss_colorWithRed:162 green:162 blue:162] forState:(UIControlStateNormal)];
        [_newsbutton setTitleColor:[UIColor ss_colorWithRed:53 green:161 blue:241] forState:(UIControlStateSelected)];
        [_newsbutton addTarget:self action:@selector(fourButtonTouchMetod:) forControlEvents:(UIControlEventTouchUpInside)];
        _newsbutton.selected = NO;
    }
    return _newsbutton;
}
- (UILabel *)lineLabel{
    if (!_lineLabel) {
        _lineLabel = [[UILabel alloc] init];
        _lineLabel.backgroundColor = [UIColor ss_colorWithRed:53 green:161 blue:241];
        
    }
    return _lineLabel;
    
}
@end
